<?php

namespace app\depend;



/**
 * 日志专用类  公用
 * mod by falcon
 *
 */
class appLog
{

    /**
     * 日志方法
     * @param string $dir 日志目录
     * @param array $data 日志内容
     *
     */
    static public function appLog($dir, $data)
    {
        if (!isset($data['p'])) {
            $mongoData['p'] = 'iov';
        } else {
            $mongoData['p'] = $data['p'];
        }
        if ($dir == 'error') {
            $mongoData['c'] = 'e';
        } else {
            $mongoData['c'] = 'i';   //普通日志
        }
        $param = [];
        if (isset($_GET) && !empty($_GET)) {
            $param['get'] = $_GET;
        }
        if (isset($_POST) && !empty($_POST)) {
            $param['post'] = $_POST;
        }
        if (isset($data['dcm_no'])) {
            $mongoData['dcmNo'] = $data['dcm_no'];
        }
        if (isset($data['http-code'])) {
            if ($data['http-code'] != 200) {
                $mongoData['c'] = 'e';   //错误  http返回不是200的话
            }
        }
        if (isset($data['post-data'])) {
            $param['curl_data'] = $data['post-data'];
        }
        if (isset($data['spend-time'])) {
            $mongoData['spend'] = $data['spend-time'];
        }
        if (isset($data['requestID'])) {
            $mongoData['requestID'] = $data['requestID'];
        }
        $mongoData['u'] = isset($data['uri']) ? str_replace([APP_SERV_ROUTE], '', $data['uri']) : '-';
        $mongoData['ip'] = getClientIp();
        if (isset($data['info'])) {
            $mongoData['d'] = $data['info'];
        } else if (isset($data['desc'])) {
            $mongoData['d'] = $data['desc'];
        } else {
            $mongoData['d'] = '-';
        }
        foreach ($data as $k => $v) {
            if (is_array($v)) {
                $data[$k] = json_encode($v, JSON_UNESCAPED_UNICODE);
            }
        }
        $mongoData['r_i'] = $data;
        $mongoData['p_m'] = json_encode($param, JSON_UNESCAPED_UNICODE);
        $mongoData['f'] = isset($data['file']) ? $data['file'] : '-';
        $mongoData['l'] = isset($data['line']) ? $data['line'] : '-';
        $mongoData['t'] = date("Y-m-d H:i:s");

        //这里异步处理日志
        self::asyncLogsRecord($mongoData);

    }//end function

    //异步记录日志
    static private function asyncLogsRecord($data)
    {
        $rootPath = APP_LOG_PATH . '/async-logs';
        try {
            if (!is_dir($rootPath)) {
                @mkdir($rootPath, 0777, true);
            }
            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $file = $rootPath . '/' . date("Y-m-d");
            $res = fopen($file, 'a');
            fwrite($res, $data . PHP_EOL);
            fclose($res);
        } catch (\Exception $e) {
            //一般到这里的话 就是 硬盘没空间了才会出现的事
        }//end try

    }//end function

//    添加页面访问记录
    public static function pageViewLog($data)
    {
        $rootPath = APP_LOG_PATH . '/pv-logs';
        try {
            if (!is_dir($rootPath)) {
                @mkdir($rootPath, 0777, true);
            }
            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $file = $rootPath . '/' . date("Y-m-d");
            $res = fopen($file, 'a');
            fwrite($res, $data . PHP_EOL);
            fclose($res);
        } catch (\Exception $e) {
            //一般到这里的话 就是 硬盘没空间了才会出现的事

        }//end try
    }

    //车主账单日志专用
    public static function carownerlog($dir,$data){
        $rootPath = APP_LOG_PATH . '/'.$dir;
        try {
            if (!is_dir($rootPath)) {
                @mkdir($rootPath, 0777, true);
            }
            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $file = $rootPath . '/' . date("Y-m-d");
            $res = fopen($file, 'a');
            fwrite($res, $data . PHP_EOL);
            fclose($res);
        } catch (\Exception $e) {
            //一般到这里的话 就是 硬盘没空间了才会出现的事
        }//end try
    }//end function

    /**
     * 把数组重组成字符串
     * @param unknown $data
     */
    static private function _rebuildArray($data)
    {
        if (is_string($data)) {
            return $data . PHP_EOL;
        }

        if (is_array($data)) {
            $str = '';
            foreach ($data as $k => $v) {
                if (!is_array($v)) {
                    $str .= $k . ':' . $v . PHP_EOL;;
                } else {
                    $str .= $k . '=>{' . PHP_EOL;
                    $str .= self::_rebuildArray($v);
                    $str .= '}' . PHP_EOL;
                }
            }
        }//end else if       
        else {
            return $data . PHP_EOL;
        }
        return $str;
    }//end function

    public static function errorLog($data, $file = "", $line = "")
    {
        !empty($file) && $data['file'] = $file;
        !empty($line) && $data['line'] = $line;
        self::appLog('error', $data);
    }


    /**
     * @param $dir
     * @param $data
     * 这个是强制在本地写文件
     */
    static public function fileLog($dir, $data)
    {

        $rootPath = APP_LOG_PATH . '/' . $dir;
        if (!is_dir($rootPath)) {
            @mkdir($rootPath, 0777, true);
        }

        $file = $rootPath . '/' . date("Y-m-d");
        $res = fopen($file, 'a');

        if (!$data) {
            fwrite($res, '没有数据!' . PHP_EOL);
        } else {
            $content = self::_rebuildArray($data);
            fwrite($res, $content);
        }
        fwrite($res, '记录时间:' . date("Y-m-d H:i:s") . PHP_EOL);
        fwrite($res, '记录客户端IP:' . getClientIp() . PHP_EOL);
        fwrite($res, '-------------------------------------------' . PHP_EOL);
        fclose($res);

    }//end function

}//end class
